package fr.cpe.dp.projet2.rmi.service;

import fr.cpe.dp.projet2.controller.Step2MusicController;
import fr.cpe.dp.projet2.factory.GenericFactory;
import fr.cpe.dp.projet2.main.Step2App;
import fr.cpe.dp.projet2.model.algo.IAlgorithm;
import fr.cpe.dp.projet2.model.music.IMusic;
import fr.cpe.dp.projet2.model.music.MusicType;
import fr.cpe.dp.projet2.model.musicBuilder.IMusicBuilder;
import fr.cpe.dp.projet2.model.musicPlayer.ConsoleMusicPlayer;
import fr.cpe.dp.projet2.model.musicPlayer.IMusicPlayer;
import fr.cpe.dp.projet2.model.musicPlayer.MusicPlayerObservable;
import fr.cpe.dp.projet2.model.shape.IShape;
import fr.cpe.dp.projet2.rmi.IRmiMusicService;
import fr.cpe.dp.projet2.view.Step2MusicPlayerView;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.JFrame;

public class RmiStep2MusicService implements IRmiMusicService {
    @Override
    public JFrame getIHM() {
        try {
            GenericFactory<IMusicBuilder> musicBuilderFactory = new GenericFactory<IMusicBuilder>("fr.cpe.dp.projet2.model.musicBuilder");
            GenericFactory<IShape> shapeFactory = new GenericFactory<IShape>("fr.cpe.dp.projet2.model.shape");
            GenericFactory<IAlgorithm> algorithmFactory = new GenericFactory<IAlgorithm>("fr.cpe.dp.projet2.model.algo");
            GenericFactory<IMusic> musicFactory = new GenericFactory<>("fr.cpe.dp.projet2.model.music");

            Map<String,IMusicBuilder> mapMusicBuilders = new HashMap<>();
            mapMusicBuilders.put("Algo aléatoire", musicBuilderFactory.createObject("RandomBuilder"));
            mapMusicBuilders.put("Algo d'une gamme", musicBuilderFactory.createObject("RangeBuilder"));

            Map<String,MusicType> mapMusicTypes = new HashMap<>();
            mapMusicTypes.put("Un fichier midi", MusicType.FILE_TYPE);
            mapMusicTypes.put("Générer une musique", MusicType.GENERATED);

            Map<String,IShape> mapShapeOptions = new HashMap<>();
            mapShapeOptions.put("Formes ovales", shapeFactory.createObject("OvalShape"));
            mapShapeOptions.put("Formes rectangulaires", shapeFactory.createObject("RectangleShape"));

            Map<String,IAlgorithm> mapAlgorithmOptions = new HashMap<>();
            mapAlgorithmOptions.put("Algorithme aléatoire", algorithmFactory.createObject("RandomAlgorithm"));

            Step2MusicPlayerView step2MusicPlayerView = new Step2MusicPlayerView(
                mapShapeOptions.keySet().toArray(new String[1]),
                mapAlgorithmOptions.keySet().toArray(new String[1])
            );

            IMusicPlayer musicPlayer = new MusicPlayerObservable(new ConsoleMusicPlayer());

            Step2MusicController step2MusicController = new Step2MusicController(
                step2MusicPlayerView,
                musicFactory,
                musicPlayer,
                mapMusicBuilders,
                mapMusicTypes,
                mapShapeOptions,
                mapAlgorithmOptions
            );
            return step2MusicPlayerView;
        } catch (MidiUnavailableException | InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
            Logger.getLogger(Step2App.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

}
